float Monkey_L = 0;
float Monkey_R = 0;
float Monkey_T = 0;
float Monkey_B = 0;

float xB = 20.0f*wpx;
float yB = 22.0f*hpx;
float xA = 14.0f*wpx;
float yA = 22.0f*hpx;

bool IsRightArea(int a)
{
    if(Monkey.Area >= a-8 && Monkey.Area <= a+8)
        return true;
    else
        return false;
}

void CheckArea()
{
    float pos = -1;
    int area = 0;
    
    while(pos < Monkey.Pos.x)
    {
        area++;
        pos += 0.5f;
    }

    Monkey.Area = area;
}

void CheckBanana()
{
    bool pass;
    
    for(int i = 0; i < coinid; i++)
    {
        if(!IsRightArea(Rcon[i].area) || Rcon[i].x == -999)
            continue;
        
        pass = false;

        if((Monkey_R >= Rcon[i].x && Monkey_R <= Rcon[i].x + xB) || (Monkey_L >= Rcon[i].x && Monkey_L <= Rcon[i].x + xB))
            if((Monkey_T >= Rcon[i].y && Monkey_T <= Rcon[i].y + yB) || (Monkey_B >= Rcon[i].y && Monkey_B <= Rcon[i].y + yB))
                pass = true;
        
        if(pass)
        {
            Rcon[i].x = -999;
            Monkey.Bananas++;
        }
    }
}

void UpdateCoords()
{
    Monkey_L = Monkey.Pos.x;
    Monkey_R = Monkey.Pos.x + xA;
    Monkey_T = Monkey.Pos.y + yA;
    Monkey_B = Monkey.Pos.y;
}

void Gravity()
{
    
    if(Monkey.Pos.y < -0.3f)
    {
        Monkey.Death = true;
        return;
    }
    
    float fspeed = 2.7f*dt;
    bool pass_r, pass_l;
    
    for(int i = 0; i < blkid; i++)
    {
        if(!IsRightArea(Rblk[i].area))
           continue;

        pass_l = false;
        pass_r = false;

        if(Monkey_L > Rblk[i].x && Monkey_L < Rblk[i].x + xB)
            pass_l = true;
        if(Monkey_R > Rblk[i].x && Monkey_R < Rblk[i].x + xB)
            pass_r = true;


        if(pass_l || pass_r)
        {
            if(Monkey_B == Rblk[i].y + yB)
            {
                Monkey.OnGround = true;
                Monkey.JumpHeight = 0;
                return;
            } else if(Monkey_B - fspeed < Rblk[i].y + yB && Monkey_B > Rblk[i].y)
            {
                if(!Monkey.InJump)
                        Monkey.Pos.y = Rblk[i].y + yB;
                return;
            }
        }
    }
    
    Monkey.OnGround = false;
    if(!Monkey.InJump)
        Monkey.Pos.y -= fspeed;
}

bool CollideTop()
{
    bool pass_r, pass_l;
    float Monkey_H = Monkey.Pos.y + yA + 0.12f;
    float Monkey_R2 = Monkey.Pos.x + xA;
    
    for(int i = 0; i < blkid; i++)
    {
        if(!IsRightArea(Rblk[i].area))
            continue;
        
        pass_r = false;
        pass_l = false;
        
        if(Monkey_L > Rblk[i].x && Monkey_L < Rblk[i].x + xB)
            pass_l = true;
        else if(Monkey_R2 > Rblk[i].x && Monkey_R2 < Rblk[i].x + xB)
            pass_r = true;
        
        if(pass_r || pass_l)
        {
            if(Monkey_H > Rblk[i].y && Monkey_H < Rblk[i].y + yB)
            {
                Monkey.Pos.y = Rblk[i].y - yA;
                return true;
            }
        }
    }
    return false;
}

void MonkeyJump()
{
    if((!Monkey.OnGround && Monkey.JumpHeight == 0) || Monkey.JumpHeight > 68*hpx || CollideTop())
    {
        Monkey.InJump = false;
        Monkey.JumpHeight = 0;
        return;
    }

    Monkey.Pos.y += 2.5f*dt;
    Monkey.JumpHeight += 2.5f*dt;
    Monkey.InJump = true;
}

void MonkeyNoJump()
{
    if(Monkey.InJump)
        Monkey.InJump = false;
}

bool CollideRight()
{
    bool pass_t, pass_b;
    
    for(int i = 0; i < blkid; i++)
    {
        if(!IsRightArea(Rblk[i].area))
            continue;
        
        if(Monkey.Pos.x < 0)
            return true;
        
        pass_t = false;
        pass_b = false;
        
        if(Monkey_B >= Rblk[i].y && Monkey_B <= Rblk[i].y + (yB - 2*hpx))
            pass_b = true;
        else if(Monkey_T > Rblk[i].y && Monkey_T < Rblk[i].y + yB)
            pass_t = true;
        
        if(pass_t || pass_b)
        {
            if(Monkey_R == Rblk[i].x)
                return true;
            else if(Monkey_R + (1.33f*dt) > Rblk[i].x && Monkey_R < Rblk[i].x)
            {
                Monkey.Pos.x = Rblk[i].x - xA;
                return true;
            }
        }
    }
    return false;
}

bool CollideLeft()
{
    bool pass_t, pass_b;
    
    for(int i = 0; i < blkid; i++)
    {
        if(!IsRightArea(Rblk[i].area))
            continue;
        
        if(Monkey.Pos.x < 0)
            return true;
        
        pass_t = false;
        pass_b = false;
        
        if(Monkey_B >= Rblk[i].y && Monkey_B <= Rblk[i].y + (yB - 2*hpx))
            pass_b = true;
        else if(Monkey_T > Rblk[i].y && Monkey_T < Rblk[i].y + yB)
            pass_t = true;
        
        if(pass_t || pass_b)
        {
            if(Monkey_L == Rblk[i].x + xB)
                return true;
            else if(Monkey_L - (1.13f*dt) < Rblk[i].x + xB && Monkey_L > Rblk[i].x)
            {
                Monkey.Pos.x = Rblk[i].x + xB;
                return true;
            }
        }
    }
    return false;
}

void MonkeyTurnRight()
{
    if(!CollideRight())
    {
        float dist = 0.8f*dt;
        
        Monkey.Pos.x += dist;
        Monkey.WalkSt++;
        
        if(Monkey.Pos.x > 1.7f)
            Monkey.Cam.x += dist;
    }
    
    if(Monkey.State == MONKEY_RIGHT)
    {
        if(Monkey.WalkSt > 40)
        {
            Monkey.State = MONKEY_RIGHT2;
            Monkey.WalkSt = 0;
        }
    } else if(Monkey.State == MONKEY_RIGHT2)
    {
        if(Monkey.WalkSt > 40)
        {
            Monkey.State = MONKEY_RIGHT;
            Monkey.WalkSt = 0;
        }
    } else
        Monkey.State = MONKEY_RIGHT;
}

void MonkeyTurnLeft()
{
    if(!CollideLeft())
    {
        float dist = 0.8f*dt;
        Monkey.Pos.x -= dist;
        Monkey.WalkSt++;
        
        if(Monkey.Pos.x > 1.7f)
            Monkey.Cam.x -= dist;
    }
    
    if(Monkey.State == MONKEY_LEFT)
    {
        if(Monkey.WalkSt > 40)
        {
            Monkey.State = MONKEY_LEFT2;
            Monkey.WalkSt = 0;
        }
    } else if(Monkey.State == MONKEY_LEFT2)
    {
        if(Monkey.WalkSt > 40)
        {
            Monkey.State = MONKEY_LEFT;
            Monkey.WalkSt = 0;
        }
    } else
        Monkey.State = MONKEY_LEFT;
}

bool IsDeath()
{
    if(Monkey.Pos.y < -2.0f && Monkey.Pos.y > -3.0f)
        Monkey.Death = true;
    
    if(Monkey.Death)
    {
        Monkey.LockMove = true;
        
        if(Monkey.Pos.y < 3.0f)
            Monkey.Pos.y += 6.0f*dt;
        else {
            Monkey.Death = false;
            Monkey.Pos.x = Rmap.spawn_x;
            Monkey.Pos.y = Rmap.spawn_y;
            Monkey.Cam.x = xx;
            Monkey.Cam.y = yy;
            Monkey.Lives--;
            
            if(Monkey.Lives != -1)
                levelsplash = 0;
        }
        return true;
    }
    return false;
}